package com.nut.article.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS);

    public static final DateTimeFormatter DAY = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter YYYYMMDD = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static final DateTimeFormatter YYYYMMDD_HHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static final DateTimeFormatter YYYYMM = DateTimeFormatter.ofPattern("yyyyMM");

    public static final DateTimeFormatter yyMMdd = DateTimeFormatter.ofPattern("yyMMdd");

    public static final String toStringDay(Integer val) {

        String str = val.toString();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            if (i == 4 || i == 6) {
                sb.append("-");
            }
            sb.append(str.charAt(i));
        }

        return sb.toString();
    }

    public static final Integer getOutDay(Integer inDay, Integer days) {
        LocalDate parse = LocalDate.parse(inDay.toString(), YYYYMMDD);
        LocalDate localDate = parse.plusDays(days);
        return Integer.valueOf(YYYYMMDD.format(localDate));
    }

    public static final String getDepDay(Integer inDay, Integer days) {
        LocalDate parse = LocalDate.parse(inDay.toString(), YYYYMMDD);
        LocalDate localDate = parse.plusDays(days);
        return DAY.format(localDate);
    }

    public static final String getNowDateTime() {
        //线程安全
        return FORMATTER.format(LocalDateTime.now());
    }

    public static final String getDateTimeStr(LocalDateTime date) {
        //线程安全
        return YYYYMMDD_HHmmss.format(date);
    }

    public static final String getNowDate() {
        //线程安全
        return YYYYMMDD.format(LocalDateTime.now());
    }

    public static final String getDatePlus(int days) {
        //线程安全
        return YYYYMMDD.format(LocalDateTime.now().plusDays(days));
    }

    public static final String getNowMonth() {
        //线程安全
        return YYYYMM.format(LocalDateTime.now());
    }

    public static final String getYYMMdd(int days) {
        //线程安全
        return yyMMdd.format(LocalDateTime.now().plusDays(days));
    }

    /**
     * 将  2020-06-30 12:12:12  --->  20200630
     * 将  2020-06-30  --->  20200630
     *
     * @param val
     * @return
     */
    public static final Integer toIntDay(String val) {
        val = val.split(" ")[0];
        return Integer.valueOf(val.replace("-", ""));
    }

    public static final Date plusMinutes(Integer min) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.get(Calendar.MINUTE);
        cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + -10);
        Date time = cal.getTime();
        return time;
    }

    /**
     * 比较相差几天
     *
     * @param arr 入住时间  yyyy-MM-dd
     * @param dep 离开时间  yyyy-MM-dd
     * @return
     */
    public static final int getDays(String arr, String dep) {
        LocalDate start = LocalDate.parse(arr, DAY);
        LocalDate end = LocalDate.parse(dep, DAY);
        return (int) start.until(end, ChronoUnit.DAYS);
    }

    public static void main(String[] args) {


        String qq = YYYYMMDD.format(LocalDateTime.now().plusDays(-1));
        System.out.println(qq);

        String arr = "2020-07-30";
        String dep = "2020-08-31";
        System.out.println(getDays(arr, dep));

        arr = "2020-08-30";
        dep = "2020-08-31";
        System.out.println(getDays(arr, dep));

        arr = "2020-08-20";
        dep = "2020-08-31";
        System.out.println(getDays(arr, dep));

        arr = "2020-09-20";
        dep = "2020-08-31";
        System.out.println(getDays(arr, dep));


        LocalDateTime now = LocalDateTime.now();
        System.out.println(getDateTimeStr(now));

        System.out.println(toIntDay("2020-06-30 12:12:12"));
        System.out.println(toIntDay("2020-06-30"));

        LocalDate parse1 = LocalDate.parse("2020-06-30", DAY);
        LocalDate parse2 = LocalDate.parse("2020-07-01", DAY);
        Period between2 = Period.between(parse1, parse2);
        System.out.println(between2.getDays());

        LocalDate of1 = LocalDate.of(2020, 6, 30);
        LocalDate of2 = LocalDate.of(2020, 7, 1);
        Period between = Period.between(of2, of1);
//        Period between = Period.between(of1, of2);
        System.out.println(between.getDays());


        Date date = DateUtil.plusMinutes(-10);

        System.out.println(date);
        System.out.println(date);
        System.out.println(date);


        System.out.println(getNowMonth());

        LocalDate localDate = LocalDate.parse("2020-04-06", DAY);
        LocalDate localDate1 = localDate.plusDays(2);
        FORMATTER.format(localDate1);


        System.out.println(localDate.plusDays(2));

        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            toStringDay(20200512);
        }
        long len = System.currentTimeMillis() - start;
        System.out.println("len=" + len);

        start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
//            toStringDay2(20200512);
        }
        len = System.currentTimeMillis() - start;
        System.out.println("len2=" + len);

//        String s = toStringDay(20200512);
//        System.out.println(s);
    }


}
